<template>
  <div class="upload-container">
    <el-upload
      class="upload-wrapper"
      action="http://localhost:8090/upload/image"
      name="multipartFile"
      list-type="picture-card"
      :on-success="handleSuccess"
      :on-preview="handlePictureCardPreview"
      :on-remove="handleRemove"
      :limit="9"
      multiple
      :on-exceed="handleExceed">
      <i class="el-icon-plus"></i>
    </el-upload>
    <el-dialog :visible.sync="dialogVisible">
      <img width="100%" :src="dialogImageUrl" alt="">
    </el-dialog>
  </div>
</template>

<script>
import eventBus from '../../assets/js/eventBus'
export default {
  name: 'ImageUpload',
  data () {
    return {
      imageList: [],
      dialogImageUrl: '',
      dialogVisible: false
    }
  },
  methods: {
    /**
     * 上传成功
     * @param res
     * @param file
     */
    handleSuccess: function (res, file, fileList) {
      file.name = res.name
      file.url = res.url
      // 传图片名称
      eventBus.$emit('setImageName', res.fileName)
    },
    /**
     * 删除文件
     * @param file
     * @param fileList
     */
    handleRemove (file, fileList) {
      console.log(file, fileList)
    },
    /**
     * 预览图片
     * @param file
     */
    handlePictureCardPreview (file) {
      console.log('预览图片')
      this.dialogImageUrl = file.url
      this.dialogVisible = true
    },
    handleExceed (files, fileList) {
      // this.$message.warning(`当前限制选择9个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`)
      this.$message.warning(`最多只能上传9张图片`)
    }
  }
}
</script>

<style scoped>
  .upload-container{
  }
  .upload-wrapper{
    padding: 24px;
    border: 1px solid #ebebeb;
    border-radius: 3px;
    box-shadow: 1px 2px 3px #CCC;
    min-height: 300px;
  }
</style>
